package com.th.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.Service.CustomerService;
import com.th.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/authc")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @GetMapping("/customerlist")
    public String list(Model model, @RequestParam(value = "pagenum", defaultValue = "1") String pageNum) {
        //查询全部
        IPage<Customer> page = customerService.page(new Page<>(Integer.parseInt(pageNum), 10));
        model.addAttribute("page", page);
        return "authc/customer";
    }

    @PostMapping("/customeradd")
    @ResponseBody
    public String customerAdd(String name, String address, String phonenumber){
        Customer customer = new Customer();
        customer.setName(name);
        customer.setAddress(address);
        customer.setPhonenumber(phonenumber);
        customerService.save(customer);
        return "1";
    }

    @GetMapping("/customerdelete")
    @ResponseBody
    public String delete(String id) {
        Boolean result = customerService.removeById(Integer.parseInt(id));
        if (result) {
            return "1";
        }
        return "0";
    }
}
